package com.foreveross.fetl.web.action.preprocess;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dayatang.querychannel.support.Page;
import com.foreveross.fetl.application.IAirLineCountApplication;
import com.foreveross.fetl.common.HBaseTable;
import com.foreveross.fetl.common.vo.AirLineCountVO;
import com.foreveross.fetl.util.DateUtils;
import com.foreveross.fetl.web.action.BaseAction;

@Controller("airLineCountAction")
@Scope("prototype")
public class AirLineCountAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3180207704750945252L;
	
	@Inject
	private IAirLineCountApplication airLineCountApplication;

	public String list() {
		return "METHOD";
	}
	public String channelList() {
		return "METHOD";
	}
	public String AirLineList() {
		return "METHOD";
	}
	
	@SuppressWarnings("unchecked")
	public String dayLines(){
		String date=getRequest().getParameter("date");
		String type=getRequest().getParameter("type");
		String tableName=HBaseTable.TABLE_AIRLINE_COUNT;
		if("double".equals(type)){
			tableName=HBaseTable.TABLE_AIRLINE_COUNT_DOUBLE;
		}
		Date day=DateUtils.parse(date, "yyyy-MM-dd");
		List<AirLineCountVO> list;
		List<AirLineCountVO> dayChannel;
		try {
			list = airLineCountApplication.findWeekList(day, tableName);
			dataMap.put("listDay", list);
			dayChannel=airLineCountApplication.findChannelList(day, tableName);
			try {
				Collections.sort(dayChannel, new Comparator(){
					@Override
					public int compare(Object o1, Object o2) {
						AirLineCountVO vo1=(AirLineCountVO)o1;
						AirLineCountVO vo2=(AirLineCountVO)o2;
						return(vo1.getTripsSize().compareTo(vo2.getTripsSize()));
					}
					
				});
				Collections.reverse(dayChannel);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			dataMap.put("channelSize", dayChannel.size());
			int lineSize=0;
			for(AirLineCountVO vo:dayChannel){
				lineSize+=vo.getTripsSize();
			}
			dataMap.put("lineSize", lineSize);
			dataMap.put("dayChannel", dayChannel);
		} catch (Exception e) {
			e.printStackTrace();
			dataMap.put("result", e.getMessage());
		}
		return "JSON";
	}
	
	public String pageJson() {
		String date=getRequest().getParameter("date");
		String type=getRequest().getParameter("type");
		String channel=getRequest().getParameter("channel");
		String method=getRequest().getParameter("method");
		String fromCity=getRequest().getParameter("fromCity");
		String toCity=getRequest().getParameter("toCity");
		if(StringUtils.isBlank(channel))
			channel=null;
		if(StringUtils.isBlank(fromCity))
			fromCity=null;
		if(StringUtils.isBlank(toCity))
			toCity=null;
		
		String tableName=HBaseTable.TABLE_AIRLINE_COUNT;
		if("double".equals(type)){
			tableName=HBaseTable.TABLE_AIRLINE_COUNT_DOUBLE;
		}
		
		Date day=null;
		if(!StringUtils.isBlank(date))
			day=DateUtils.parse(date, "yyyy-MM-dd");
		Page<AirLineCountVO> all=new Page<AirLineCountVO>();
		if("channel".equals(method)){
			all=airLineCountApplication.pageForChannel(day, tableName, channel, page, pagesize);
		}else if("airLine".equals(method)){
			all=airLineCountApplication.pageForAirLine(day, tableName, channel, fromCity,
					toCity, page, pagesize);
		}
		dataMap.put("Rows", all.getResult());
		dataMap.put("start", page * pagesize - pagesize);
		dataMap.put("limit", pagesize);
		dataMap.put("Total", all.getTotalCount());
		return "JSON";
	}
	
}
